const app = getApp()
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    newList:[],
    list: [],
    informLlist:[],
    deptList:[[]],
    load: true,
    news:{},
    nextNews:{}
  },
  onLoad() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    var that = this
    let list = [{}];
    let newList = [{}];
    let informList = [{}];
    let deptList = [[{}]]
    wx.request({
      url: 'http://localhost:8081/test/news',
      method:'GET',
      success(res){
        newList = res.data.news
        informList = res.data.inform
        let k =0
        let j =1
        list[0] = {}
        list[0].id=0
        list[0].name = informList[0].dept
        console.log(informList.length)
        console.log(list[0].name)
        deptList[0][0] = informList[0]
        console.log(deptList[0][0])
        for(let i=1;i<informList.length;i++){
          if(informList[i].dept!=informList[i-1].dept){
            k++
            list[k] = {}
            list[k].id = k
            list[k].name = informList[i].dept    
          }
          console.log(informList[i])
          if(deptList[k]==null){
            deptList[k] = [{}]
            deptList[k][0]=informList[i]
            j=1
          }else{
            deptList[k][j] = informList[i]
            j++
          }
          console.log("for")
        }
        that.setData({
          list: list,
          listCur: list[0],
          newList: newList,
          deptList: deptList
        })
        
      }
      
    })
    
   
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target,
      news: e.currentTarget.dataset.news
    })
    console.log(123);
  },
  tapNav(e){
    console.log(e.currentTarget.dataset.next)
    this.setData({
      nextNews: e.currentTarget.dataset.next
    })
   
    wx.navigateTo({
      url: '/pages/plugin/News/news?next='+JSON.stringify(this.data.nextNews),
    })
    console.log("123")
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  onReady() {
    wx.hideLoading()
  },
  tabSelect(e) {
    console.log(e.currentTarget.dataset.id)
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
    })
  },
  VerticalMain(e) {
    let that = this;
    let list = this.data.list;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + list[i].id);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;     
        }).exec();
      }
      that.setData({
        load: false,
        list: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: (list[i].id - 1) * 50,
          TabCur: list[i].id
        })
        return false
      }
    }
  }
})